/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 *     @: data can be read
 *     =: data can be written
 * +----------------------------------------------------------------------------------------------+
 * |========================|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|========================|
 * +----------------------------------------------------------------------------------------------+
 * ^                        ^                                            ^                        ^
 * start                   out                                           in                      end
 * 
 */


#include <kernel/kfifo.h>
#include <string.h>
#include <mm/mem.h>


/**
 * @brief alloc a kfifo struct
 * @param fifo  pointer to kfifo struct
 * @param size  size of fifo, must be power of 2
 * @return 0 on success, -1 on error
 */
int kfifo_alloc(struct kfifo *fifo, size_t size)
{
    fifo->data = kmalloc(size);
    if (!fifo->data) {
        return -1;
    }

    fifo->size = size;
    fifo->mask = size - 1;
    fifo->in = fifo->out = 0;
    return 0;
}


/**
 * @brief free a kfifo struct
 * @param fifo  pointer to kfifo struct
 * @return none
 */
void kfifo_free(struct kfifo *fifo)
{
    fifo->size = 0;
    fifo->mask = 0;
    fifo->in = fifo->out = 0;
    kfree(fifo->data);
    fifo->data = NULL;
}


/**
 * @brief push data to kfifo
 * @param fifo  pointer to kfifo struct
 * @param data  pointer to data
 * @param len   length of data
 * @return length of data push
 */
size_t kfifo_push(struct kfifo *fifo, const void *data, size_t len) 
{
    size_t avail = kfifo_unused(fifo);
    size_t to_write = min(len, avail);

    if (to_write == 0) {
        return 0;
    }

    size_t in = fifo->in & fifo->mask;
    size_t first_part = min(to_write, fifo->size - in);
    // TODO: add memory barrier for SMP ARCH

    memcpy((uint8_t*)(fifo->data) + in, data, first_part);
    if (to_write > first_part) {
        memcpy(fifo->data, (uint8_t*)data + first_part, to_write - first_part);
    }

    // TODO: add memory barrier for SMP ARCH
    fifo->in = (fifo->in + to_write) & fifo->mask;
    return to_write;
}


/**
 * @brief pop data from kfifo
 * @param fifo  pointer to kfifo struct
 * @param data  pointer to data
 * @param len   length of data
 * @return length of pop data
 */
size_t kfifo_pop(struct kfifo *fifo, void *data, size_t len) 
{
    size_t to_read = min(len, kfifo_used(fifo));

    if (to_read == 0) {
        return 0;
    }

    size_t out = fifo->out & fifo->mask;
    size_t first_part = min(to_read, fifo->size - out);
    // TODO: add memory barrier for SMP ARCH

    memcpy(data, (uint8_t*)(fifo->data) + out, first_part);
    if (to_read > first_part) {
        memcpy((uint8_t *)data + first_part, fifo->data, to_read - first_part);
    }

    // TODO: add memory barrier for SMP ARCH
    fifo->out = (fifo->out + to_read) & fifo->mask;
    return to_read;
}
